package com.futuristik.objects;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.Manifold;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.futuristik.arkitek.Character;
//ADDED
import com.badlogic.gdx.graphics.Texture.TextureWrap;

public class Flag extends ArkitekObject {
	public final static float width = 0.5f;
	public final static float height = 0.5f;
	
	TextureRegion sprite, winImage;
	public FlagType type;
	public enum FlagType{
		SPAWN(0), END(1), CHECKPOINT(2);
		
		private int value;

		private FlagType (int value) {
			this.value = value;
		}

		public int getValue () {
			return value;
		}
	}
	
	public Flag(float x, float y, FlagType type){
		super(x, y);
		this.type = type;
		
		if(type.equals(FlagType.SPAWN)){
			sprite = new TextureRegion(new Texture(Gdx.files.internal("data/obj/greenflag.png"))); 
		}
		else if(type.equals(FlagType.END)){
			sprite = new TextureRegion(new Texture(Gdx.files.internal("data/obj/redflag.png"))); 
		}
		else{
			sprite = new TextureRegion(new Texture(Gdx.files.internal("data/obj/yellowflag.png"))); 
		}
	}

	public Flag(float x, float y, float type){
		this(x, y, (type == 0) ? FlagType.SPAWN : ((type == 1) ? FlagType.END : FlagType.CHECKPOINT));
	}
	
	public void create(World w){
		BodyDef def = new BodyDef(); //initialize Body definition to begin the creation
		def.position.set(new Vector2(x, y));
		objectBody = w.createBody(def); //creates a body from the definition and adds it to the World's list of bodies
 
		PolygonShape poly = new PolygonShape();		
		poly.setAsBox(width, height, new Vector2(width, height),  angle); //creates the polygon as a box of given width and height
		FixtureDef fDef = new FixtureDef();
			//fDef.filter.maskBits = 0x000;
			fDef.friction = 0;
			fDef.shape = poly;
		objectBody.createFixture(fDef); //this attaches the "fixture" to the body
	}
	
	public boolean prePlayerContact(Character player, Contact cont, Manifold oldManifold){
		cont.setEnabled(false);
		if(type.equals(FlagType.END)){
			return true;
      }
		else{
			player.lastCheckpoint = this;
		}
		return false;
	}
	
	public void render(SpriteBatch batch){
		batch.draw(sprite, x, y, 1, 1, 1, 1, 1, 1, 0);    
	}
	
	public String toString(){
		return "Flag "+x+" "+y+" "+type.getValue();
	}
	
	public float getX() {
		return x;
	}

	public float getY() {
		return y;
	}

	public FlagType getType() {
		return type;
	}
}